package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.BaseContext;
import com.itheima.dto.OrdersDto;
import com.itheima.mapper.OrdersMapper;

import com.itheima.pojo.*;

import com.itheima.service.*;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private UserService userService;
    @Autowired
    private ShoppingCartService shoppingCartService;


    @Override
    public void submit(Orders orders) {
        Long userId= BaseContext.get();  //用户id
        Long addressBookId = orders.getAddressBookId();// 地址id
        //自己定义一个id 用雪花算法自己生成一个id
        long ordersId = IdWorker.getId();
        BigDecimal total =new BigDecimal(0);
        //用户
        User user = userService.getById(userId);
        //地址
        AddressBook addressBook = addressBookService.getById(addressBookId);
        //购物车
        QueryWrapper<ShoppingCart> wrapper =new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(wrapper);


        //1.组装订单明细表数据 (n条)
        ArrayList<OrderDetail> orderDetailList = new ArrayList<>();
        for (ShoppingCart cart : shoppingCartList) {
            //将每一个cart转换成orderDetail
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(cart.getName());
            orderDetail.setOrderId(ordersId);
            orderDetail.setDishId(cart.getDishId());
            orderDetail.setSetmealId(cart.getSetmealId());
            orderDetail.setDishFlavor(cart.getDishFlavor());
            orderDetail.setNumber(cart.getNumber());
            orderDetail.setAmount(cart.getAmount().multiply(new BigDecimal(cart.getNumber())));
            total=total.add(orderDetail.getAmount());
            orderDetail.setImage(cart.getImage());

            orderDetailList.add(orderDetail);
        }

        //2.组装订单表 数据 (1条)

        orders.setId(ordersId);
        orders.setNumber(System.currentTimeMillis()+"");
        orders.setStatus(2);
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(total);
        orders.setUserName(user.getName());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setConsignee(addressBook.getConsignee());

        //保存orders数据
        this.save(orders);
        //保存orderDetail数据
        orderDetailService.saveBatch(orderDetailList);
        //清空购物车
        shoppingCartService.remove(wrapper);
    }

    @Override
    public Page<OrdersDto> pageInfo(String number, Integer page, Integer pageSize) {
        //创建条件对象
        QueryWrapper<Orders> wrapper =new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(number),"number",number);
        Page<Orders> pageInfo1 = this.page(new Page<>(page, pageSize), wrapper);
        //创建page2
        Page<OrdersDto> pageInfo2 = new Page<>(page,pageSize);
        //拷贝
        BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");
        for (Orders record : pageInfo1.getRecords()) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(record,ordersDto);


        }
        return null;
    }
}
